<?php
namespace app\api\model;

use think\Model;
use think\db;

class Sign extends Model
{
    /**
     * 查询用户的签到状况
     * @param $userid 用户id
     */
    public function index($userid)
    {
        $data = Db::name('sign')->where('userid',$userid)->select();
        if(count($data)==0)
        {
            return 0;
        }
        else
        {
            return $data;
        }
    }

    /**
     * 用户签到
     * @param array $userid 用户id
     */
    public function add($userid)
    {
            $data = Db::name('sign')->where('userid',$userid)->select();
            if(count($data) == 0)  //没有该用户的签到记录
            {
                $query4 = Db::name('sign')->insert(['times'=>date('Y-m-d H:i:s'),'userid'=>$userid,'days'=>1,'number'=>'15','one'=>date('d',time())]);
                return 1;
            }
            else
            {
                //判断今天是否签到
                $todayBegin=date('Y-m-d'." 00:00:00");
                $todayEnd= date('Y-m-d'." 23:59:59");
                $isexit = Db::name('sign')->field('times')->where(['userid'=>$userid])->where('times','between',[$todayBegin,$todayEnd])->select();
                if(count($isexit) == 1)   //今日已签到
                {
                    return 0;
                }
                else    //今日未签到
                {
                    $times = Db::name('sign')->where('userid',$userid)->field('times')->select();
                    $time = strtotime($times[0]['times']);

                    if((time()-$time > 24*60*60))       //上次签到时间大于24小时，连续签到天数清零
                    {
                        $query = Db::name('sign')->where('userid',$userid)->update(['days'=>1]);
                    }
                    else     //上次签到时间小于24小时，连续签到次数加1
                    {
                        $query = Db::name('sign')->where('userid',$userid)->setInc('days');
                    }
                    //更新上次签到时间和签到积分，并生成一条积分流水
                    $query1 = Db::name('sign')->where('userid',$userid)->update(['times'=>date('Y-m-d H:i:s')]);
                    $query2 = Db::name('sign')->where('userid',$userid)->setInc('number', 15);
                    $query3 = Db::name('userwater')->insert(['userid'=>$userid,'style'=>1,'status'=>1,'amout'=>15,'adopt'=>1,'comments'=>'签到','times'=>date('Y-m-d H:i:s')]);

                    $sqldate = date('m',$time);    //上次签到日期的月份
                    $nowdate = date('m',time());  //当前月份
                    //记录本次签到日期
                    if($sqldate != $nowdate)  //上次签到日期与本次签到日期月份不一样
                    {
                        $oldtime = $times[0]['times'];
                        $onetime=date("Y-m-d H:i:s", strtotime("-1 month"));  //获取前1个月的时间，获取格式为2016-12-30 13:26:13
                        $twotime=date("Y-m-d H:i:s", strtotime("-2 month"));  //获取前2个月的时间
                        $threetime=date("Y-m-d H:i:s", strtotime("-3 month"));  //获取前3个月的时间

                        $rs = Db::name('sign')->where('userid',$userid)->field('one,two,three')->select();

                        if($oldtime < $onetime && $oldtime >= $twotime)     //月份间隔 大于1个月，小于2个月
                        {
                            $one =  date('d',time());
                            $two = $rs[0]['one'];
                            $three = $rs[0]['two'];
                        }
                        elseif($oldtime < $twotime && $oldtime >= $threetime) //月份间隔 大于2个月，小于3个月
                        {
                            $one =  date('d',time());
                            $two = '';
                            $three = $rs[0]['one'];
                        }
                        elseif($oldtime < $threetime)  //月份间隔 大于3个月
                        {
                            $one =  date('d',time());
                            $two = '';
                            $three = '';
                        }
                        $query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$one,'two'=>$two,'three'=>$three]);
                    }
                    else  //上次签到日期与本次签到日期月份一样
                    {
                        $one = Db::name('sign')->where('userid',$userid)->field('one')->select();
                        $arr[] = $one[0]['one'];
                        $arr[] = date('d',time());
                        $newones = implode(",",$arr);
                        $query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$newones]);
                    }
                return 1;
                }
            }
    }



}